package com.hdax.dm.controller;

import com.hdax.dm.dto.OrderDto;
import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.exception.DmException;
import com.hdax.dm.service.OrderService;
import com.hdax.dm.service.SubmitOrderService;
import com.hdax.dm.utils.Token.TokenUtil;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import com.hdax.dm.vo.SubmitOrderVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SubmitOrderService submitOrderService;

    /**
     *
     * 根据订单号查询订单详情
     * */

    @PostMapping("p/queryorderlist")
    private CommonResponse<List<OrderDto>> queryorderlist(@RequestBody Map<String,Object> map,HttpServletRequest request){
        String token= request.getHeader("token");
        Integer id= Integer.valueOf(TokenUtil.parseToken(token));
        return orderService.queryorderlist(map,Integer.valueOf(id));
    }

    /**
    *
    * 获取用户提交的订单信息
    * */

    @PostMapping("submitorder")
    private CommonResponse<Map<String,String>> submitorder(@RequestBody SubmitOrderVo orderVo,HttpServletRequest request){
        try {
            String token= request.getHeader("token");
            Long uid= Long.valueOf(TokenUtil.parseToken(token));
            orderVo.setUserId(uid);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return submitOrderService.submitorder(orderVo);
    }

    //异常处理
    @ExceptionHandler(value = {Exception.class})
    public CommonResponse<String> dmExceptionHandler(Exception e){
        return ResponseUtil.returnFail(e.hashCode()+"",e.getMessage());
    }
}
